//
//  NSObject+HDAddNewComManager.m
//  app
//
//  Created by D on 15/8/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDAddNewComManager.h"
#import "HDSelectModel.h"
#import "HDHouseModel.h"
#import "HDBulidingModel.h"
#import "SNFmdbManager.h"

@interface HDAddNewComManager ()

@property (nonatomic,strong) JKHttpClient * jkHttp;
@property (nonatomic,strong) SNUniteUrl * uniteUrl;
@property (nonatomic,strong) NSMutableArray * mutableArray;
@property (nonatomic,strong) SNFmdbManager * snFmdbManager;

@end

@implementation HDAddNewComManager

+(HDAddNewComManager *)shareManager
{
    AS_SINGLETON(HDAddNewComManager, manager, {
        
        manager = [[HDAddNewComManager alloc]init];
        manager.jkHttp = [[JKHttpClient alloc]init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.mutableArray = [NSMutableArray array];
        manager.snFmdbManager = [SNFmdbManager shareManager];
        
    })
    return manager;
}

-(void)getCommunityMessage:(HDAddNewComResult *)result Params:(NSString*)params Sign:(NSString *)sign
{
    if ([self.jkHttp isRunning]) {
        return;
    }
    NSString * url = [NSString string];
    if (sign.integerValue == 0) {
        url = self.uniteUrl.my_community_list;
    }else if (sign.integerValue == 1)
    {
        url = [NSString stringWithFormat:@"%@%@",self.uniteUrl.my_community_grouplist,params];
        params = @"4";
    }else if (sign.integerValue == 2)
    {
         url = [NSString stringWithFormat:@"%@%@",self.uniteUrl.my_community_buildinglist,params];
    }else
    {
        url = [NSString stringWithFormat:@"%@%@",self.uniteUrl.my_community_floorlist,params];
    }
    
    [self.jkHttp get:url  result:^(JKResponse *resp) {
       
        if (resp.OK) {
            result.success = YES;
            DLog(@"%d",[resp.content[@"list"]count]);
            result.dataSourceArray = [self communityDataSourceWithArray:resp.content[@"list"] WithSign:sign];
            
        }else if (resp.failure){
            result.failure = YES;
        }
        [result invoke];
        
    }];
    
}

-(void)submitFirst:(HDAddNewComResult*)result Params:(NSDictionary*)params
{
    if ([self.jkHttp isRunning]) {
        return;
    }
    
    [self.jkHttp get:self.uniteUrl.my_apply_house params:params  result:^(JKResponse *resp) {
        
        if (resp.OK) {
            result.success = YES;
            
        }else if (resp.failure){
            result.failure = YES;
        }
        [result invoke];
        
    }];
}

-(void)submitEnd:(HDNewComSubmitResult*)result Params:(NSDictionary*)params NewComDic:(NSDictionary*)newComDic
{
    if ([self.jkHttp isRunning]) {
        return;
    }
//    http://api.mylife114.com/
    NSString * url = [NSString stringWithFormat:@"%@?cmid=%@&id=%@&smscode=%@selected=%@",self.uniteUrl.my_add_house,[params objectForKey:@"cmid"],[params objectForKey:@"id"],[params objectForKey:@"smscode"],[params objectForKey:@"selected"]];
    
    [self.jkHttp get:self.uniteUrl.my_add_house params:params  result:^(JKResponse *resp) {
        
        if (resp.OK) {
            result.success = YES;
            [self updataSaveComWith:newComDic];
//            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"havenewcom"];
//            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }else if (resp.failure){
            result.failure = YES;
        }
        [result invoke];
        
    }];

}
-(void)updataSaveComWith:(NSDictionary*)newComDic
{
    [self.snFmdbManager.fmdbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        NSString * sql = [NSString string];
        
        sql = [NSString stringWithFormat:@"UPDATE Community_%@ SET selected = '%@' WHERE id = '%@' ",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"],@"0",[[NSUserDefaults standardUserDefaults]objectForKey:@"currentCom"]];
        
        if ([db executeUpdate:sql]) {
            DLog(@"更新community表成功");
            
            NSString * sql = [NSString stringWithFormat:@"insert into Community_%@(id  ,cmid ,community_name  ,house_name , selected , delSign )values (?, ?, ?, ?, ?, ?);",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]] ;
            
            NSArray * dataArray = @[[newComDic objectForKey:@"id"],[newComDic objectForKey:@"cmid"],[newComDic objectForKey:@"community_name"],[newComDic objectForKey:@"house_name"],@"1",@"000"];
            
            if ([db executeUpdate:sql withArgumentsInArray:dataArray]) {
                DLog(@"保存到了 mycommunity表");
            }

            
            
        }
        
    }];
}



-(NSMutableArray*)communityDataSourceWithArray:(NSArray*)array WithSign:(NSString*)sign
{
    [self.mutableArray removeAllObjects];
    
//    if (sign.integerValue <= 1) {
        for (int i = 0 ; i < array.count; i++) {
            NSDictionary * dict = [array objectAtIndex:i];
            HDSelectModel * model = [HDSelectModel modelWithDict:dict WithSign:sign];
            [self.mutableArray addObject:model];
            
        }
//    }
//    else {
//    
//        for (int i = 0 ; i < array.count; i++) {
//            NSDictionary * dict = [array objectAtIndex:i];
////            self.mutableArray = [self buildingOrHouseArray:dict[@"building_list"] Sign:sign];
//            [self.mutableArray addObjectsFromArray:[self buildingOrHouseArray:dict[@"building_list"] Sign:sign]];
//            
//        }
//    }

    
    
    return self.mutableArray;
}

-(NSMutableArray*)buildingOrHouseArray:(NSArray*)array Sign:(NSString*)sign
{
    NSMutableArray * tempArr = [NSMutableArray array];
    if (sign.integerValue == 2) {
        for (int i = 0 ; i < array.count; i++) {
            NSDictionary * dict = [array objectAtIndex:i];
            HDBulidingModel * model = [HDBulidingModel modelWithDict:dict];
            [tempArr addObject:model];
            
        }

    }
    else if (sign.integerValue == 3){
        for (int i = 0 ; i < array.count; i++) {
            NSDictionary * dict = [array objectAtIndex:i];
            tempArr = [self housrArr:dict[@"house_list"]];
            [tempArr addObjectsFromArray:[self housrArr:dict[@"house_list"]]];
        }
    }
    
    return tempArr;
}
-(NSMutableArray*)housrArr:(NSArray*)array
{
    NSMutableArray * tempArr = [NSMutableArray array];
    for (int i = 0 ; i < array.count; i++) {
        NSDictionary * dict = [array objectAtIndex:i];
        HDHouseModel * model = [HDHouseModel modelWithDict:dict];
        [tempArr addObject:model];
        
    }

    return tempArr;
}

@end











